﻿using MySql.Data.MySqlClient;

namespace BookMS
{
    class Dao
    {
        private MySqlConnection conn;

        public MySqlConnection Connect()
        {
            string str = "Server=localhost;Database=BookDB;User ID=root;Password=lz7781157;";
            conn = new MySqlConnection(str);
            conn.Open();
            return conn;
        }

        public MySqlCommand Command(string sql)
        {
            return new MySqlCommand(sql, Connect());
        }

        public int Execute(string sql)  // 更新操作
        {
            using (var cmd = Command(sql))
            {
                return cmd.ExecuteNonQuery();
            }
        }

        public MySqlDataReader read(string sql)  // 读取操作
        {
            return Command(sql).ExecuteReader();
        }

        public void DaoClose()
        {
            if (conn != null && conn.State == System.Data.ConnectionState.Open)
            {
                conn.Close(); // 关闭数据库连接
            }
        }
    }
}
